package com.suneast.suneaststore.modules.personal.order.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.bean.personal.order.ListBean;
import com.suneast.suneaststore.ui.customview.RoundImageView;

import java.util.List;

public class FastGoodsAdapter extends BaseAdapter<FastGoodsAdapter.ViewHolder, ListBean.GoodsBean> {

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fast_order_goods, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ListBean.GoodsBean goods = list.get(position);
        holder.updateImage(goods.getImages());
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private RecyclerView rvGoods;
        private GoodsPhotoAdapter adapter;
        ViewHolder(View itemView) {
            super(itemView);
            rvGoods = itemView.findViewById(R.id.rv_goods_photo);
            rvGoods.setLayoutManager(new LinearLayoutManager(itemView.getContext(), LinearLayoutManager.HORIZONTAL, false));
            adapter = new GoodsPhotoAdapter();
            rvGoods.setAdapter(adapter);
        }

        void updateImage(List<String> urls) {
            adapter.setList(urls);
        }

        private class GoodsPhotoAdapter extends BaseAdapter<GoodsPhotoAdapter.InnerViewHolder, String> {
            @NonNull
            @Override
            public InnerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new InnerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fast_order_goods_photo, parent, false));
            }

            @Override
            public void onBindViewHolder(@NonNull InnerViewHolder holder, int position) {
                Glide.with(holder.ivPhoto.getContext()).load(list.get(position)).into(holder.ivPhoto);
            }

            class InnerViewHolder extends RecyclerView.ViewHolder {
                RoundImageView ivPhoto;
                InnerViewHolder(View itemView) {
                    super(itemView);
                    ivPhoto = itemView.findViewById(R.id.cv_goods);
                }
            }
        }
    }
}
